<template>
  <div class="vehicle-container">
    <el-row class="header">
      <el-card>
        <el-form label-width="auto">
          <el-row type="flex">
            <el-form-item label="车辆类型" style="margin: 0">
              <el-col>
                <el-select
                  v-model="form.truckTypeId"
                  clearable
                  class="inputStyle"
                  placeholder="请选择车型编号"
                >
                  <el-option
                    v-for="item in vehicleTypeData"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  />
                </el-select>
              </el-col>
            </el-form-item>
            <el-form-item label="车辆号码" style="margin: 0">
              <el-col>
                <el-input
                  v-model="form.licensePlate"
                  clearable
                  class="inputStyle"
                  placeholder="请输入车型号码"
                />
              </el-col>
            </el-form-item>
            <el-form-item style="margin: 0">
              <template>
                <el-button
                  class="buttonStyle"
                  size="small"
                  type="primary"
                  @click="searchClick"
                >查询</el-button>
                <el-button
                  class="buttonStyle"
                  size="small"
                  @click="resetClick"
                >重置</el-button>
              </template>
            </el-form-item>
          </el-row>
        </el-form>
      </el-card>
    </el-row>
    <el-row class="tabs">
      <el-card>
        <el-row>
          <div
            v-for="(item, index) in tabs"
            :key="item.name"
            :class="{ active: item.id === active }"
            class="tabs-module"
            @click="tabsClick(index)"
          >
            <span>{{ item.name }}</span>
            <span>{{
              !item.id
                ? tableData.backupData.length
                : item.id == "1"
                  ? usable
                  : outOfService
            }}</span>
          </div>
        </el-row>
      </el-card>
    </el-row>
    <el-row class="footer">
      <el-card class="footer-card">
        <el-row>
          <el-button
            size="small"
            type="primary"
            class="buttonStyle"
            @click="addVehicle"
          >新增车型</el-button>
        </el-row>
        <el-row>
          <el-table :data="tableData.data" stripe style="width: 100%">
            <el-table-column label="序号" width="50" align="center">
              <template slot-scope="scope">
                <span>{{ scope.$index + 1 }}</span>
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              prop="licensePlate"
              label="车辆号码"
              width="138.2"
            />
            <el-table-column
              align="center"
              prop="truckTypeName"
              label="车辆类型"
              width="138.2"
            />
            <el-table-column
              align="center"
              prop="driverNum"
              label="司机数量"
              width="138.2"
            />
            <el-table-column
              align="center"
              prop="workStatus"
              label="车辆状态"
              width="138.2"
            >
              <template #default="{ row }">
                <div class="workStatus">
                  <span
                    :style="{
                      'background-color': row.workStatus ? '#189450' : 'red',
                    }"
                    class="dot"
                  />
                  <span v-if="row.workStatus">可用</span>
                  <span v-else>停用</span>
                </div>
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              prop="deviceGpsId"
              label="GPS设备ID"
              width="138.2"
            />
            <el-table-column
              align="center"
              prop="allowableLoad"
              label="实载重量 (T)"
              width="138.2"
            />
            <el-table-column
              align="center"
              prop="allowableVolume"
              label="实载体积（方）"
              width="138.2"
            />
            <el-table-column align="center" prop="address" label="操作">
              <template #default="{ row }">
                <el-row type="flex" align="middle">
                  <el-button
                    type="text"
                    size="small"
                    @click="goDetail(row)"
                  >查看详情</el-button>
                  <div class="centerLine" />
                  <el-button
                    type="text"
                    size="small"
                    style="color: red"
                    @click="startStop(row)"
                  >停用</el-button>
                  <div class="centerLine" />
                  <el-button
                    type="text"
                    size="small"
                    @click="matchingDrivers(row)"
                  >匹配司机</el-button>
                </el-row>
              </template>
            </el-table-column>
            <template slot="empty">
              <el-empty :image-size="150" description="暂无数据" />
            </template>
          </el-table>
        </el-row>
        <el-row
          type="flex"
          justify="center"
          align="middle"
          style="margin-top: 10px; height: 40px"
        >
          <el-pagination
            :current-page="form.page"
            :page-sizes="[10, 20, 30, 40]"
            :page-count="pages"
            :page-size="form.pageSize"
            :pager-count="7"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="sizeChange"
            @current-change="gurrentChange"
          />
        </el-row>
      </el-card>
    </el-row>
    <!-- 停用 & 启用 -->
    <el-dialog class="dialog" title="提示" :visible.sync="visible" width="30%">
      <div>确定停用吗？车辆停用需满足以下条件</div>
      <div class="dialog-content">
        <span>1.车辆当前无运输任务</span>
        <span>2.车辆无关联运输任务</span>
      </div>
      <div style="color: rgb(255, 179, 2)">注：停用后司机将自动解除绑定</div>
      <el-row type="flex" justify="end" style="margin-top: 10px">
        <el-button size="small" @click="vehicleStauts = false">取 消</el-button>
        <el-button
          size="small"
          class="dialog-button"
          type="primary"
          @click="vehicleStauts"
        >确 定</el-button>
      </el-row>
    </el-dialog>
    <!-- 匹配司机 -->
    <el-dialog
      title="车辆配置"
      :visible.sync="outerVisible"
      class="outerDialog"
      width="30%"
    >
      <div class="header">
        <div>确定停用吗？车辆停用需满足以下条件</div>
        <div class="dialog-content">
          <span>1.车辆当前无运输任务</span>
          <span>2.车辆无关联运输任务</span>
        </div>
      </div>
      <el-dialog
        width="40%"
        class="innerDialog"
        title="配置司机"
        :visible.sync="innerVisible"
        append-to-body
      >
        <div class="header">
          <el-descriptions :column="2">
            <el-descriptions-item label="车牌号">{{
              vehicleData && vehicleData.licensePlate
            }}</el-descriptions-item>
            <el-descriptions-item label="车型">{{
              vehicleData && vehicleData.truckTypeName
            }}</el-descriptions-item>
            <el-descriptions-item label="车辆状态">
              <el-tag
                :type="vehicleData.workStatus ? 'success' : 'danger'"
                size="small"
              >{{
                vehicleData && vehicleData.workStatus ? "可用" : "停用"
              }}</el-tag>
            </el-descriptions-item>
            <el-descriptions-item label="实载重量">{{
              vehicleData && vehicleData.allowableLoad
            }}</el-descriptions-item>
            <el-descriptions-item label="实载体积">{{
              vehicleData && vehicleData.allowableVolume
            }}</el-descriptions-item>
          </el-descriptions>
        </div>
        <div class="main">
          <el-select
            v-model="driverSelect"
            value-key="id"
            clearable
            multiple
            placeholder="请选择"
          >
            <el-option
              v-for="item in driverList"
              :key="item.userId"
              :label="item.name"
              :value="item.userId"
            />
          </el-select>
        </div>
        <div class="footer">
          <el-table :data="driverList" style="width: 100%">
            <el-table-column align="center" label="序号">
              <template slot-scope="scope">
                <span>{{ scope.$index + 1 }}</span>
              </template>
            </el-table-column>
            <el-table-column align="center" prop="name" label="司机名称" />
            <el-table-column align="center" prop="address" label="操作">
              <el-row type="flex" justify="center" align="middle">
                <el-button size="small" type="text">查看</el-button>
                <div class="centerLine" />
                <el-button
                  size="small"
                  type="text"
                  style="color: red"
                >删除</el-button>
              </el-row>
            </el-table-column>
            <template slot="empty">
              <el-empty :image-size="60" description="暂无数据" />
            </template>
          </el-table>
        </div>
        <div slot="footer">
          <el-row type="flex" justify="center">
            <el-button
              style="width: 70px; background-color: #189450"
              size="small"
              type="success"
            >确定</el-button>
            <el-button style="width: 70px" size="small">取消</el-button>
          </el-row>
        </div>
      </el-dialog>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="outerVisible = false">取 消</el-button>
        <el-button
          size="small"
          type="primary"
          class="dialog-button"
          @click="outerDialogVisible"
        >确定</el-button>
      </div>
    </el-dialog>
    <!-- 新增车辆 -->
    <el-dialog
      class="addDialog"
      title="新增车辆"
      :visible.sync="dialogFormVisible"
      width="35%"
    >
      <el-form ref="addDialogForm" :model="addVehicleForm" :rules="rules" label-width="auto">
        <el-form-item label="车辆类型" prop="licensePlate">
          <el-select
            v-model="addVehicleForm.truckTypeId"
            clearable
            placeholder="请选择"
            class="dialogInputStyle"
          >
            <el-option
              v-for="item in vehicleType"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="车牌号码" prop="truckTypeId">
          <el-input
            v-model="addVehicleForm.licensePlate"
            class="dialogInputStyle"
            placeholder="请输入车牌号码"
            autocomplete="off"
            clearable
          />
        </el-form-item>
        <el-form-item label="GPS设备ID" prop="deviceGpsId">
          <el-input
            v-model="addVehicleForm.deviceGpsId"
            class="dialogInputStyle"
            placeholder="请输入GPS设备ID"
            autocomplete="off"
            clearable
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          size="small"
          class="buttonStyle"
          type="primary"
          @click="addDialogConfirm"
        >确 定</el-button>
        <el-button
          size="small"
          class="buttonStyle"
          @click="addDialogCancel"
        >取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getVehicleAPI,
  getVehicleTypeAPI,
  // disableVehicleAPI,
  getVehicleTypeListAPI,
  getbindingDriverAPI,
  addVehicleAPI
} from '@/api/vehicle'
export default {
  data() {
    return {
      // 弹窗
      visible: false,
      // 匹配司机
      outerVisible: false,
      innerVisible: false,
      // 司机列表
      driverSelect: [],
      driverList: [],
      // 表单弹框
      dialogFormVisible: false,
      // 车辆类型数据
      vehicleTypeData: [],
      // 车辆类型列表
      vehicleType: [],
      // 车辆数据
      vehicleData: {},
      // 表单数据
      form: {
        licensePlate: '',
        page: 1,
        pageSize: 10,
        truckTypeId: '',
        workStatus: ''
      },
      // 新增车辆表单数据
      addVehicleForm: {
        licensePlate: '',
        truckTypeId: '',
        deviceGpsId: ''
      },
      rules: {
        licensePlate: [
          { required: true, message: '请选择车辆类型', trigger: 'blur' }
        ],
        truckTypeId: [
          { required: true, message: '请输入车辆号码', trigger: 'blur' }
        ],
        deviceGpsId: [
          { required: true, message: '请输入GPS设备ID', trigger: 'blur' }
        ]
      },
      // 表格数据
      tableData: {
        data: [],
        backupData: []
      },
      // tabs选中
      active: 0,
      // tabs数据渲染
      tabs: [
        {
          name: '全部',
          id: 0
        },
        {
          name: '可用',
          id: 1
        },
        {
          name: '停用',
          id: 2
        }
      ],
      // 总页数
      total: 0,
      // 总页数
      pages: 1,
      // 防抖
      timer: null
    }
  },
  computed: {
    // 可用
    usable() {
      return this.tableData.backupData.filter((item) => item.workStatus).length
    },
    // 停用
    outOfService() {
      return this.tableData.backupData.filter((item) => !item.workStatus)
        .length
    }
  },
  async created() {
    await this.tableListData()
    this.vehicleTypeList()
    if (!this.active) {
      const page = 1
      const pageSize = this.total
      getVehicleAPI({ page, pageSize }).then(({ data }) => {
        this.tableData.backupData = data.data.items
      })
    }
  },
  methods: {
    // 车辆列表
    async tableListData() {
      const { data } = await getVehicleAPI(this.form)
      const itemData = data.data
      this.pages = +itemData.pages
      this.total = +itemData.counts
      this.tableData.data = itemData.items
    },
    // 车辆类型
    async vehicleTypeList() {
      const { data } = await getVehicleTypeAPI()
      this.vehicleTypeData = data.data
    },
    // tabs
    tabsClick(index) {
      this.active = index
      clearTimeout(this.timer)
      this.timer = setTimeout(() => {
        if (this.active === 1) {
          this.form.workStatus = '1'
        } else if (this.active === 2) {
          this.form.workStatus = '0'
        } else {
          this.form.workStatus = null
        }
        this.tableListData()
      }, 700)
    },
    // 搜索
    searchClick() {
      clearTimeout(this.timer)
      this.timer = setTimeout(() => {
        this.tableListData()
      }, 500)
    },
    // 重置
    resetClick() {
      this.form.licensePlate = null
      this.form.truckTypeId = null
      clearTimeout(this.timer)
      this.timer = setTimeout(() => {
        this.tableListData()
      }, 500)
    },
    // 停用 && 启用{未完成}
    startStop(row) {
      this.visible = true
      this.vehicleData = row
    },
    // 新增车辆
    async addVehicle() {
      const { data } = await getVehicleTypeListAPI()
      this.vehicleType = data.data
      this.dialogFormVisible = true
    },
    // 新增车辆弹窗-确认
    async addDialogConfirm() {
      await this.$refs.addDialogForm.validate()
      await addVehicleAPI(this.addVehicleForm)
      this.$refs.addDialogForm.resetFields()
      this.tableListData()
      this.dialogFormVisible = false
    },
    // 新增车辆弹窗-取消
    addDialogCancel() {
      this.$refs.addDialogForm.resetFields()
      this.dialogFormVisible = false
    },
    // 查看详情
    goDetail(row) {
      this.$router.push({
        path: '/vehicle/vehicleDetail/' + row.id
      })
    },
    // 匹配司机
    matchingDrivers(row) {
      this.outerVisible = true
      this.vehicleData = row
    },
    // 匹配司机弹窗-确认
    async outerDialogVisible() {
      this.innerVisible = true
      const { data } = await getbindingDriverAPI(this.vehicleData.id)
      this.driverList = data.data
      this.driverSelect = this.driverList.map((item) => item.userId)
    },
    // 停用 && 启用{未完成}
    async vehicleStauts() {
      // const result = await disableVehicleAPI(this.vehicleId)
      // console.log(result)
    },
    // 当前页最大条数
    sizeChange(pageSize) {
      this.form.pageSize = pageSize
      this.tableListData()
    },
    // 当前页
    gurrentChange(page) {
      this.form.page = page
      this.tableListData()
    }
  }
}
</script>

<style scoped lang="scss">
.vehicle-container {
  padding: 0 20px;
  .header,
  .tabs,
  .footer {
    margin-bottom: 20px;
    font-size: 13px;
  }

  .header {
    .el-form-item {
      padding: 0 20px;
      .inputStyle {
        width: 300px;
      }
    }
  }
  .tabs {
    .active {
      color: #67c23a;
      background: #f0f9eb;
      border: 1px solid #c2e7b0;
      border-color: #c2e7b0;
    }
    ::v-deep .el-card__body {
      padding: 0 !important;
      .tabs-module {
        display: inline-block;
        cursor: pointer;
        width: 120px;
        height: 48px;
        text-align: center;
        line-height: 48px;
      }
    }
  }
  .footer {
    .footer-card {
      padding: 10px;
      padding-bottom: 0;

      .centerLine {
        box-sizing: border-box;
        margin: 0 10px;
        width: 1px;
        height: 13px;
        background: #d0d3da;
      }

      .workStatus {
        display: flex;
        align-items: center;
        justify-content: center;
        .dot {
          display: inline-block;
          transform: translateX(-60%);
          width: 5px;
          height: 5px;
          border-radius: 50%;
        }
      }
    }
  }
  .dialog {
    .dialog-content {
      margin: 15px 0 10px;
      display: flex;
      justify-content: space-between;
    }

    ::v-deep .el-dialog__body {
      padding: 10px 20px 20px;
      font-size: 13px;
    }

    .dialog-button {
      background-color: #189450;
      border-color: #189450;
    }
  }

  .outerDialog {
    .dialog-content {
      margin: 15px 0 10px;
      display: flex;
      justify-content: space-between;
    }
    .dialog-button {
      background-color: #189450;
      border-color: #189450;
    }
  }
}
// 公用样式
.buttonStyle {
  width: 80px;
  height: 30px;
  margin-bottom: 15px;
  &.buttonStyle:first-child {
    background-color: #189450;
    border-color: #189450;
    margin-right: 10px;

    &:hover {
      opacity: 0.7;
    }
  }
}
// 新增车辆
.addDialog {
  ::v-deep .el-dialog {
    border-radius: 10px;
  }
  ::v-deep .el-dialog__header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 15px 20px;
    border-radius: 10px 10px 0 0;
    background-color: #f3f4f7;
  }
  ::v-deep .el-dialog__title {
    font-size: 15px;
    font-weight: bold;
  }
  ::v-deep .el-dialog__headerbtn {
    position: static;
    background: 0 0;
    border: none;
    outline: 0;
    cursor: pointer;
    font-size: 16px;
  }
  ::v-deep .el-dialog__body {
    display: flex;
    justify-content: center;
    padding: 20px 25px 20px;
    font-size: 13px;
  }
}
.dialogInputStyle {
  width: 320px;
  height: 30px;
}
::v-deep .el-form-item__label {
  font-size: 13px;
}

::v-deep .el-table thead tr th {
  font-size: 13px;
  font-weight: bold;
  background: #f8faff !important;
  border: none;
}
::v-deep .el-table tbody tr td {
  font-size: 13px;
  color: #606266;
  border: none;
}
::v-deep .el-input__inner {
  height: 35px;
  font-size: 13px;
}
::v-deep .el-pagination .el-select .el-input .el-input__inner {
  height: 28px;
  font-size: 13px;
}

::v-deep .el-descriptions {
  padding: 0 40px;
  background-color: #fbfafa;
}
::v-deep .el-descriptions__body {
  margin-bottom: 15px;
  font-size: 13px;
  background-color: #fbfafa;
}
.innerDialog {
  ::v-deep .el-dialog__header {
    background-color: #f3f4f7;
  }
  .main {
    margin-bottom: 15px;
  }
  .centerLine {
    display: inline-block;
    margin: 0 6px;
    width: 1px;
    height: 13px;
    background: #d0d3da;
  }
}
</style>
